package com.forum.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.InitializingBean;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "upload")
public class UploadConfig implements InitializingBean {
    private String path;
    private String allowedTypes;
    private Long maxSize;
    
    /**
     * 当bean初始化完成后被调用，用于检查配置是否加载
     */
    @Override
    public void afterPropertiesSet() {
        log.info("UploadConfig initialized with path: {}", path);
        log.info("UploadConfig initialized with allowedTypes: {}", allowedTypes);
        log.info("UploadConfig initialized with maxSize: {}", maxSize);
        
        if (allowedTypes == null) {
            log.warn("allowedTypes is null, will use default values");
        }
        
        if (maxSize == null) {
            log.warn("maxSize is null, will use default value: 2MB");
        }
        
        if (path == null) {
            log.warn("path is null, will use default value: E:/upload/forum/images/");
        }
    }

    /**
     * 获取允许的文件类型列表
     * 如果配置为空，则提供默认值
     */
    public List<String> getAllowedTypesList() {
        // 处理allowedTypes为null的情况，提供默认值
        if (allowedTypes == null || allowedTypes.isEmpty()) {
            log.warn("Using default allowed types: image/jpeg, image/png, image/gif, image/jpg");
            return Arrays.asList("image/jpeg", "image/png", "image/gif", "image/jpg");
        }
        log.debug("Using configured allowed types: {}", allowedTypes);
        return Arrays.asList(allowedTypes.split(","));
    }
} 